package com.cnpc.rightslogan.controller;

import com.cnpc.rightslogan.entity.User;
import com.cnpc.rightslogan.service.IUserService;
import com.cnpc.rightslogan.utils.common.BaseController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2017/9/13.
 */
@RestController
@RequestMapping("userSimple")
@Api("UserSimpleController相关的api")
@Slf4j
public class UserSimpleController extends BaseController {
    @Autowired
    private IUserService userSimpleServiceImpl;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getOrderUser")
    @HystrixCommand(fallbackMethod = "getOrderUserFallback")
    public String getOrderUser() {
        return restTemplate.getForEntity("http://rightslogan-dictionary/api/dictionary/getDictionaryList", String.class).getBody();
    }

    public String getOrderUserFallback() {
        log.warn("http://rightslogan-dictionary/api/dictionary/getDictionaryList调用失败!");
        return "http://rightslogan-dictionary/api/dictionary/getDictionaryList调用失败";
    }

    @ApiOperation(httpMethod = "GET", value = "根据id查询user信息", notes = "查询数据库中某个的user信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "userId", paramType = "path", dataType = "Integer")
    })
    @ResponseBody
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable int id) {
        log.info("开始查询某个user信息");
        return userSimpleServiceImpl.getUser(id);
    }

    @ApiOperation(httpMethod = "POST", value = "新增用户信息", notes = "新增用户信息")
    @ResponseBody
    @PostMapping("/user")
    public Integer addUser(@RequestBody @ApiParam(name = "User", value = "User对象") User user) {
        log.info("开始新增用户信息");
        return userSimpleServiceImpl.addUser(user);
    }

    @ApiOperation(httpMethod = "PUT", value = "修改用户信息", notes = "修改用户信息")
    @ResponseBody
    @PutMapping("/user")
    public Integer updateUser(@RequestBody @ApiParam(name = "User", value = "User对象") User user) {
        log.info("开始修改用户信息");
        return userSimpleServiceImpl.updateUser(user);
    }

    @ApiOperation(httpMethod = "DELETE", value = "删除用户", notes = "删除用户")
    @ResponseBody
    @DeleteMapping("/user")
    public Integer getUser(@RequestBody @ApiParam(name = "User", value = "User对象") User user) {
        log.info("开始删除用户");
        return userSimpleServiceImpl.deleteUser(user);
    }

}